package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.RenRZuBillDao;
import com.cjzuji.rental.mapper.OrderBillMapper;
import com.cjzuji.rental.pojo.OrderBill;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class RenRZuBillDaoImpl implements RenRZuBillDao {
    private static final Logger logger = LoggerFactory.getLogger(RenRZuBillDaoImpl.class);
    @Autowired
    OrderBillMapper orderBillMapper;
    @Override
    @Transactional
    public void save( List<Map<String, Object>> billData, Long orderID) {
        logger.info("账单信息添加开始{}", billData.toString());
        List<OrderBill>  billList = new ArrayList<>();
        for (Map<String, Object> item : billData){
            //logger.info("账单信息添加开始{}", item.toString());
            //租期
            String zuqi=item.get("期数").toString();
            String term = zuqi.split("/")[0];
            OrderBill orderBill = OrderBill.builder()
                    .orderId(orderID)
                    .ordersId(item.get("订单").toString().substring(0, 16))
                    .realPayTime(item.get("实际支付日").toString())
                    .term(Integer.parseInt(term))   //租期
                    .realPayCapital(Double.parseDouble(item.get("实付金额 表示用户实际支付租金；当账单为【支付完成】状态时，实付金额+支付优惠金额=账单金额； 平台以【实付金额】进行租金结算；").toString()))  //实付金额
                    .capital(Double.parseDouble(item.get("账单金额").toString()))      //应还金额
                    .monthRepayment(Double.parseDouble(item.get("账单金额").toString()))  //月租金
                    .zfbDiscountAmt(0.0)  //支付宝优惠金额
                    .ovdueBuyoutAmt(0.0)     //逾期买断金
                    .writeOffAmt(0.0)      //优惠金额
                    .reduceAmt(0.0)      //减免金额
                    .overdueAmount(0.0)       //逾期违约金
                    .repayDateFmt(item.get("预计扣款日").toString())  //还款日
                    .realPayTradeId(" ") // 商家交易号
                    .repayStatusStr(item.get("支付状态").toString())  //还款状态
                    .realRepayTime(item.get("实际支付日").toString())  //还款时间
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            //logger.info("打印添加参数2 {}",orderBill);
            billList.add(orderBill);
        }

        try {
            System.out.println("批量保存：~~~~~~~~~~"+ billList.toString());
            orderBillMapper.insertBatchSomeColumn(billList);
        }catch (Exception e){
            logger.info("账单信息添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }
    @Override
    public List<OrderBill> getOrderBillList(Long orderId) {
        return orderBillMapper.getOrderBillList(orderId);
    }
}